#include "func.h"
#include "mysql.h"



MYSQL * mysqlInit()     // 数据库初始化
{
    MYSQL * conn = NULL;
    char * host = "localhost";
    char * user = "root";
    char * passwd = "";
    char * db = "mysql";

    conn = mysql_init(NULL);
    if(mysql_real_connect(conn, host, user, passwd, db, 0, NULL, 0) == NULL)
        printf("error:%s\n", mysql_error(conn));
    return conn;
}

void ERROR(MYSQL * conn, int ret)       // 验证操作是否出错
{
    if(ret != 0)
    {
        printf("error query: %s\n", mysql_error(conn));
        exit(1);
    }
}

void InsertFile(MYSQL * conn, char * parent_id, char * filename, int owner_id, char * md5, int filesize, int type) // 向虚拟文件表插入数据
{
    char insert[255];
    sprintf(insert, "INSERT INTO File VALUE(id, '%s', '%s', %d, '%s', %d, %d);", parent_id, filename, owner_id, md5, filesize, type);
    ERROR(conn, mysql_query(conn, insert));
}

void InsertUser(MYSQL * conn, char * username, char * salt, char * cryptpasswd, char * path)    // 向用户表插入数据
{
    char insert[255];
    sprintf(insert, "INSERT INTO User VALUE(id, '%s', '%s', '%s', '%s');", username, salt, cryptpasswd, path);
    ERROR(conn, mysql_query(conn, insert));
}

void mydrop(MYSQL * conn, char * tableName)     // 删除表
{
    char dropfile[255];
    sprintf(dropfile, "drop table %s;", tableName);
    ERROR(conn, mysql_query(conn, dropfile));
}

void createFileTable(MYSQL * conn)  // 创建虚拟文件表
{
    char * query = "CREATE TABLE File(id int auto_increment primary key, parent_id varchar(255), filename varchar(30), owner_id int, md5 char(20), filesize int, type int);";
    ERROR(conn, mysql_query(conn, query));
}

void createUserTable(MYSQL * conn)  // 创建用户表
{
    char * query = "CREATE TABLE User(id int auto_increment primary key, username varchar(20), salt char(10), cryptpasswd varchar(20), path varchar(20));";
    ERROR(conn, mysql_query(conn, query));
}

int checkMD5(MYSQL * conn, char * filename, char * MD5) // 验证文件的MD5是否已在数据库存在
{
    char * queryMD5 = "select md5 from File where filename = '";
    char * node = "\'";
    queryMD5 = strcat(queryMD5, filename);
    queryMD5 = strcat(queryMD5, node);
    mysql_query(conn, queryMD5);
    MYSQL_RES * md5Res = mysql_store_result(conn);
    MYSQL_ROW md5Row = mysql_fetch_row(md5Res);
    if(strcmp(md5Row[0], MD5) == 0)
    {
        printf("MD5 check success\n");
        mysql_free_result(md5Res);
        return 1;
    }
    printf("MD5 check failed\n");
    mysql_free_result(md5Res);
    return 0;
}
/*
void mysqlstore(MYSQL * conn)   // select结果存储和打印
{
    MYSQL_RES * result = mysql_store_result(conn);
    if(result == NULL)
    {
        printf("error query2: %s\n", mysql_error(conn));
        exit(1);
    }

    int rows = mysql_num_rows(result);
    int cols = mysql_num_fields(result);
    printf("rows:%d, cols:%d\n", rows, cols);

    MYSQL_ROW row;
    while((row = mysql_fetch_row(result)) != NULL)
    {
        for(int i = 0; i < cols; ++i)    
            printf("%10s ", row[i]);
        printf("\n");
    }

    mysql_free_result(result);
    mysql_close(conn);
}
*/
